package com.net;




import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class Client {


    public <T>RespResult<T> exe(ReqParam param, Class<T> t) {
        RespResult<T> res=pasreResponse(param.toString(),t);
        return res;
    }

    private <T> RespResult<T> pasreResponse(String data, Class<T> t) {
        RespResult<T> res=new RespResult<T>();
        //String str="200&Success&张三&18&test1|test2|test3~test700|test7002|test7003&sign";
        int idx=data.indexOf('&');
        if(idx==-1){
        	res.code=-1000;
        	return res;
        }
//        res.code=Integer.parseInt(data.substring(0,idx));
//        //System.out.println("code="+res.code);
//        data=data.substring(idx+1,data.length());
//        idx=data.indexOf('&');
//        res.msg=data.substring(0,idx);
//        //System.out.println("msg="+res.msg);
//        data=data.substring(idx+1,data.length());
//        idx=data.lastIndexOf('&');
//        res.sign=data.substring(idx+1,data.length());
//        //System.out.println("sign="+res.sign);
//        data=data.substring(0,idx);
//        //System.out.println("data="+data);
//        if(data.indexOf('&')!=-1){
//            res.data=parserObj(data,t);
//        }else if(data.indexOf('|')!=-1){
//            //res.data=parserArray(data,t);
//        }
        return res;
    }


    private <T> T parserObj(String data, Class<T> t)  {
        String[]objArr=data.split("&");
        if(data!=null){
            try {
               Object bean= t.newInstance();
                Method method= t.getMethod("getKeySort");
                Object obj=method.invoke(bean);
                if(obj instanceof  String[]){
                    String[] keySort= (String[]) obj;
                    for (int i = 0; i < keySort.length; i++) {
                       // System.out.println("keySort "+i+" = [" + keySort[i] + "]");
                        Field field= t.getDeclaredField(keySort[i]);
                        field.setAccessible(true);
                        if(field.getType().isAssignableFrom(ArrayList.class)||field.getType().isAssignableFrom(List.class)){
                            ArrayList<Object>  ar= (ArrayList<Object> )parserArray(objArr[i],field);
                            //System.out.println("ar = [" + ar.toString() + "]");
                            field.set(bean,ar);
                        }else{
                            field.set(bean,objArr[i]);
                        }
                    }
                }
                return (T) bean;
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                System.out.println("error = [" + e.getMessage() + "] is not method getKeySort");
                return null;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }


            // Field keySor=t.getDeclaredField()
            //t.getDeclaredFields()
            //for (int i = 0; i < objArr.length; i++) {

            //}
        }


        return null;
    }

    private <T>ArrayList<T> parserArray(String data,Class<T>clazz){

        return null;
    }

    @SuppressWarnings("Since15")
    private ArrayList<?> parserArray(String data, Field field) {
        Type genType = field.getGenericType();//t.getGenericSuperclass();
        if (!(genType instanceof ParameterizedType)) {//i
            //return Object.class;
            return null;
        }
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        if(params!=null){
          //System.out.println("param0 = [" + params[0] + "]");
            Type type=params[0];
            try {//getTypeName()
                Class<?> clazz= Class.forName(type.getTypeName());
                if(!clazz.isAssignableFrom(String.class)){
                    String[]list=data.split("~");
                    ArrayList<Object> arrList=new ArrayList<Object>(list.length);
                    for (int i = 0; i < list.length; i++) {//
                        Object bean= clazz.newInstance();
                        Method method= clazz.getMethod("getKeySort");
                        Object obj=method.invoke(bean);
                        String[] keySort= (String[]) obj;
                        String str=list[i];
                       // System.out.println("sub="+str);
                        String[] objArr=str.split("\\|");
                        //System.out.println(objArr.toString());
                        for (int j = 0; j < keySort.length; j++) {
                            //System.out.printf(keySort[j]+" ;");
                            Field field1=clazz.getField(keySort[j]);
                            field1.setAccessible(true);
                            field1.set(bean,objArr[j]);
                        }
                        arrList.add(bean);
                    }
                    return arrList;
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }

        return null;
    }


}
